import controllers.*;

import errors.*;

import models.*;

import mx.controls.Alert;
import mx.events.CloseEvent;

import views.*;

/* Login & Register */
private function login() : void {
	try {
		userManager.connectUser(ui_login_email.text, ui_login_password.text);
	} catch(e:UIError) {
		UIErrorController.handleUIError(e);
	}
}

private function register() : void {
	try {
		userManager.createUser(	ui_register_name.text,
								ui_register_password.text,
								ui_register_password_confirm.text,
								ui_register_email.text);
	} catch(e:UIError) {
		UIErrorController.handleUIError(e);
	}
}

private function forgot() : void {
	WindowManager.open(WindowManager.USER_FORGOT);
}

private function editUserProfile() : void {
	WindowManager.open(WindowManager.USER_EDIT, userManager.currentUser);
}

/* Project list */
private function updateProjectList() : void {
	projectManager.updateList();
}

private function createProject() : void {
	WindowManager.open(WindowManager.PROJECT_CREATE);
}

private function inspectProject() : void {
	if(ui_project_list.selectedItem is Project) {
		projectManager.currentProject = ui_project_list.selectedItem as Project;
		WindowManager.open(WindowManager.PROJECT_DETAILS, ui_project_list.selectedItem as Project);
	} else {
		diagramManager.currentDiagram = ui_project_list.selectedItem as Diagram;
		projectManager.currentProject = projectManager.getProjectById(diagramManager.currentDiagram.projectId);
		WindowManager.open(WindowManager.DIAGRAM_DETAILS, ui_project_list.selectedItem as Diagram);
	}
}

private function editProject() : void {
	WindowManager.open(WindowManager.PROJECT_EDIT, ui_project_list.selectedItem as Project); 
}

/* Diagrams */
private function createDiagram() : void {
	WindowManager.open(WindowManager.DIAGRAM_CREATE);
}

private function editDiagram() : void {
	
	if(ui_project_list.selectedItem != null && ui_project_list.selectedItem is Diagram) { 
		WindowManager.open(WindowManager.DIAGRAM_EDIT, ui_project_list.selectedItem as Diagram);
	}
}

/* Contributors */
private function addContributor() : void {
	WindowManager.open(WindowManager.CONTRIBUTOR_ADD);
}

private function removeContributor() : void {
	if(ui_project_details_project_contributors.selectedItem) {
		Alert.show("Remove this contributor from the project ?", "Contributors", Alert.YES | Alert.NO, this, function (event:CloseEvent) : void {
			if(event.detail == Alert.YES && ui_project_details_project_contributors.selectedItem) {
				projectManager.removeContributor(projectManager.currentProject, ui_project_details_project_contributors.selectedItem as User);
			}
		});
	}
}

/* Classes */
private function updateClassList() : void {
	self().umlClassManager.updateList(projectManager.currentProject, self().umlClassManager.upadateListHandler);
}

private function createClass() : void {
	WindowManager.open(WindowManager.CLASS_CREATE);
}

/****** Switch views ******/
public function showLoginModule() : void {
	WindowManager.open(WindowManager.USER_LOGIN);
}

public function showRegisterModule() : void {
	WindowManager.open(WindowManager.USER_REGISTER);
}

public function showForgotModule() : void {
	WindowManager.open(WindowManager.USER_FORGOT);
}
